package com.youlai.boot.system.service.impl;

import com.youlai.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.WxKfMapper;
import com.youlai.boot.system.service.WxKfService;
import com.youlai.boot.system.model.entity.WxKf;
import com.youlai.boot.system.model.form.WxKfForm;
import com.youlai.boot.system.model.query.WxKfQuery;
import com.youlai.boot.system.model.vo.WxKfVO;
import com.youlai.boot.system.converter.WxKfConverter;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 客户档案服务实现类
 *
 * @author youlaitech
 * @since 2025-08-26 15:58
 */
@Service
@RequiredArgsConstructor
public class WxKfServiceImpl extends ServiceImpl<WxKfMapper, WxKf> implements WxKfService {

    private final WxKfConverter wxKfConverter;

    /**
    * 获取客户档案分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<WxKfVO>} 客户档案分页列表
    */
    @Override
    public IPage<WxKfVO> getWxKfPage(WxKfQuery queryParams) {
        Page<WxKfVO> pageVO = this.baseMapper.getWxKfPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取客户档案表单数据
     *
     * @param id 客户档案ID
     * @return
     */
    @Override
    public WxKfForm getWxKfFormData(Long id) {
        WxKf entity = this.getById(id);
        return wxKfConverter.toForm(entity);
    }
    
    /**
     * 新增客户档案
     *
     * @param formData 客户档案表单对象
     * @return
     */
    @Override
    public boolean saveWxKf(WxKfForm formData) {
        WxKf entity = wxKfConverter.toEntity(formData);
        Long userId = SecurityUtils.getUserId();
        entity.setCreateBy(userId);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateBy(userId);
        entity.setUpdateTime(LocalDateTime.now());
        return this.save(entity);
    }
    
    /**
     * 更新客户档案
     *
     * @param id   客户档案ID
     * @param formData 客户档案表单对象
     * @return
     */
    @Override
    public boolean updateWxKf(Long id,WxKfForm formData) {
        WxKf entity = wxKfConverter.toEntity(formData);
        Long userId = SecurityUtils.getUserId();
        entity.setUpdateBy(userId);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }
    
    /**
     * 删除客户档案
     *
     * @param ids 客户档案ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteWxKfs(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的客户档案数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
